package homework.exp06;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;

public class A {
    static Scanner cin = null;

    static {
        try {
            cin = new Scanner(new FileInputStream(new File("src/homework/exp06/A.in")));
        } catch (IOException e) {
            cin = new Scanner(System.in);
        }
    }


    public static void main(String[] args) {
        Map<String, Integer> VARS = new HashMap<>();
        while (cin.hasNextLine()) {
            String line = cin.nextLine();
            if (line.contains("%%%")) break;
            String[] params = line.trim().split("=");
            VARS.put(params[0].trim(), Integer.parseInt(params[1].trim()));
        }
        while (cin.hasNextLine()) {
            String infix = cin.nextLine().trim();
            String postInfix = String.valueOf(infix2postfix(infix));
            System.out.println(eval(postInfix, VARS));
        }
        if (cin != null) cin.close();
    }

    static int precedence(char op) {
        if (op == '+' || op == '-') return 1;
        if (op == '*' || op == '/') return 2;
        return 0;
    }

    static StringBuffer infix2postfix(String infix) {
        StringBuffer infix2 = new StringBuffer();
        for (char c : infix.toCharArray()) {
            String op = "+-*/()";
            if (op.indexOf(c) >= 0) {
                infix2.append(" ");
                infix2.append(c);
                infix2.append(" ");
            } else {
                infix2.append(c);
            }
        }


        Stack<String> STACK = new Stack<>();
        StringBuffer postfix = new StringBuffer();
        for (String tok : infix2.toString().trim().split("\\s+")) {
            if (!"+-*/()".contains(tok)) {
                postfix.append(" ").append(tok).append(" ");
            } else if (tok.charAt(0) == '(') {
                STACK.push(tok);
            } else if (tok.charAt(0) == ')') {
                while (!STACK.empty() && STACK.peek().equals("(")) {
                    postfix.append(" ").append(STACK.peek()).append(" ");
                    STACK.pop();
                }
                if (!STACK.empty() && STACK.peek().equals("(")) STACK.pop();
            } else {
                while (!STACK.empty() && precedence(STACK.peek().charAt(0)) >= precedence(tok.charAt(0))) {
                    postfix.append(" ").append(STACK.peek()).append(" ");
                    STACK.pop();
                }
                STACK.push(tok);
            }
        }

        while (!STACK.empty()) {
            postfix.append(" ").append(STACK.peek()).append(" ");
            STACK.pop();
        }

        return postfix;
    }

    static int eval(String exp, Map<String, Integer> VARS) {
        int ans = 0;
        Stack<Integer> STACK = new Stack<>();
        for (String tok : exp.trim().split("\\s+")) {
            if (VARS.containsKey(tok)) {
                STACK.push(VARS.get(tok));
            } else if ("+-*/".contains(tok)) {
                if (STACK.empty()) return -1;
                int b = STACK.peek();
                STACK.pop();
                if (STACK.empty()) return -1;
                int a = STACK.peek();
                STACK.pop();
                int c = 0;
                switch (tok.charAt(0)) {
                    case '+':
                        c = a + b;
                        break;
                    case '-':
                        c = a - b;
                        break;
                    case '*':
                        c = a * b;
                        break;
                    case '/':
                        c = a / b;
                        break;
                    default:
                        break;
                }
                STACK.push(c);
            }

        }
        if (STACK.size() == 1) return STACK.peek();
        else return -1;
    }

}